package a66weding.com.jiehuntong.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ScrollView;

/**
 * @version :
 * @creator : AxDu
 * @create-time : 2016/7/19
 * @description :
 */
public class ObserveScrollView extends ScrollView {

    private ScrollListener mListener;
    private View contentView;

    public static interface ScrollListener {//声明接口，用于传递数据

        public void scrollOritention(int l, int t, int oldl, int oldt);

        /**
         * Called when scroll to bottom
         */
        public void onBottom();

        /**
         * Called when scroll to top
         */
        public void onTop();
    }

    public ObserveScrollView(Context context, AttributeSet attrs,
                             int defStyle) {
        super(context, attrs, defStyle);
    }

    public ObserveScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ObserveScrollView(Context context) {
        super(context);
    }


    @Override
    protected void onScrollChanged(int l, int scroY, int oldl, int oldscroY) {
        super.onScrollChanged(l, scroY, oldl, oldscroY);
        if (mListener != null) {
            mListener.scrollOritention(l, scroY, oldl, scroY);
        }
        doOnBorderListener();
    }

    public void setScrollListener(ScrollListener l) {
        this.mListener = l;

        if (contentView == null) {
            contentView = getChildAt(0);
        }
    }


    private void doOnBorderListener() {
        if (contentView != null && contentView.getMeasuredHeight() <= getScrollY() + getHeight()) {
            if (mListener != null) {
                mListener.onBottom();
            }
        } else if (getScrollY() == 0) {
            if (mListener != null) {
                mListener.onTop();
            }
        }
    }


}
